package service

import (
	"time"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/errno"
)

// 用户收货地址信息接口
type ShopAddressResponse struct {
	ID       int64
	UID      int64
	Name     string
	Area     string
	Phone    string
	PostCode string
	Address  string
	Status   int
}

// 创建新地址
// Auth: hzp
func (address *ShopAddressResponse) Create() (err error) {

	// 校验参数
	if address.Name == "" || address.Address == "" {
		return errno.ParamsError
	}

	var AddAddress model.ShopUserAddressModel
	AddAddress.UID = address.UID
	AddAddress.Name = address.Name
	AddAddress.Area = address.Area
	AddAddress.Phone = address.Phone
	AddAddress.PostCode = address.PostCode
	AddAddress.Address = address.Address
	AddAddress.Status = address.Status
	AddAddress.CreateTime = time.Now().Unix()
	err = model.CreateShopUserAddress(AddAddress)
	if err != nil {
		return err
	}
	return nil
}

// 修改地址
// Auth: hzp
func (address *ShopAddressResponse) Update() (err error) {

	// 校验参数
	if address.Name == "" || address.Address == "" {
		return errno.ParamsError
	}

	if address.ID <= 0 {
		return errno.ParamsError
	}

	var UpdateAddress model.ShopUserAddressModel
	UpdateAddress.Name = address.Name
	UpdateAddress.Area = address.Area
	UpdateAddress.Phone = address.Phone
	UpdateAddress.PostCode = address.PostCode
	UpdateAddress.Address = address.Address
	UpdateAddress.Status = address.Status
	err = model.UpdateShopUserAddress(UpdateAddress)
	if err != nil {
		return err
	}
	return nil
}

// 删除地址
// Auth: hzp
func (address *ShopAddressResponse) Del() (err error) {

	// 参数校验
	if address.ID <= 0 || address.UID <= 0 {
		return errno.ParamsError
	}

	return model.DelShopUserAddress(address.UID, address.ID)
}

// 查询收货地址
// Auth: hzp
func (address *ShopAddressResponse) List() (res []ShopAddressResponse, err error) {

	// 参数校验
	if address.UID <= 0 {
		return res, errno.ParamsError
	}

	data, err := model.ListShopUserAddress(address.UID)
	if err != nil {
		return
	}

	for _, k := range data {
		var temp ShopAddressResponse
		temp.ID = k.ID
		temp.UID = k.UID
		temp.Address = k.Address
		temp.Name = k.Name
		temp.Phone = k.Phone
		temp.Area = k.Area
		temp.Status = k.Status
		temp.PostCode = k.PostCode
		res = append(res, temp)
	}

	return res, nil
}
